package main.java.com.lee.io;

import java.io.*;

/**
 * created by winston on 2023/11/24
 */
public class ReadWriteTest {
    public static void main(String[] args) throws IOException {
//        testRead("/Users/leejiliang/ideaproject/gitee/demo-multi-thread/hello.txt");
        testWrite("/Users/leejiliang/ideaproject/gitee/demo-multi-thread/hello.txt");
    }

    private static void testRead(String filePath) throws IOException {
        var fileReader = new FileReader(filePath);
        // 借助装饰器增强FileReader
        var bufferedReader = new BufferedReader(fileReader);
        String line ;
        while ((line = bufferedReader.readLine()) != null) {
//            System.out.println(bufferedReader.readLine());
            System.out.println(line);
        }
        bufferedReader.close();
        fileReader.close();
    }

    private static void testWrite(String filePath) throws IOException {
        var fileReader = new FileReader(filePath);
        // 借助装饰器增强FileReader
        var bufferedReader = new BufferedReader(fileReader);
        var fileWriter = new FileWriter("/Users/leejiliang/ideaproject/gitee/demo-multi-thread/hello3.txt");
        var bufferedWriter = new BufferedWriter(fileWriter);
        String line ;
        while ((line = bufferedReader.readLine()) != null) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();
        bufferedReader.close();
        fileReader.close();
    }
}
